package com.baidu.entity.vo;

import com.baidu.common.constant.CommonConstant;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.io.Serializable;

/**
 * @Author Mawq
 * @Date 2021/8/30 9:38
 * @Version 1.0
 */
@Data
public class MyResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private boolean success = true;

    private String message = "操作成功！";

    private String code = "0";

    private T data;

    private long timestamp = System.currentTimeMillis();


    public MyResult() {

    }

    public MyResult<T> success(String message) {

        this.message = message;
        this.code=String.valueOf(CommonConstant.SC_OK_200);
        this.success=true;
        return this;
    }

    @Deprecated
    public static MyResult<Object> ok() {
        MyResult<Object> r = new MyResult<Object>();
        r.setSuccess(true);
        r.setCode(String.valueOf(CommonConstant.SC_OK_200));
        r.setMessage("成功");
        return r;
    }

    @Deprecated
    public static MyResult<Object> ok(String msg) {
        MyResult<Object> r = new MyResult<Object>();
        r.setSuccess(true);
        r.setCode(String.valueOf(CommonConstant.SC_OK_200));
        r.setMessage(msg);
        return r;
    }

    @Deprecated
    public static MyResult<Object> ok(Object data) {
        MyResult<Object> r = new MyResult<Object>();
        r.setSuccess(true);
        r.setCode(String.valueOf(CommonConstant.SC_OK_200));
        r.setData(data);
        return r;
    }

    public static<T> MyResult<T> OK() {
        MyResult<T> r = new MyResult<T>();
        r.setSuccess(true);
        r.setCode(String.valueOf(CommonConstant.SC_OK_200));
        r.setMessage("成功");
        return r;
    }

    public static<T> MyResult<T> OK(T data) {
        MyResult<T> r = new MyResult<T>();
        r.setSuccess(true);
        r.setCode(String.valueOf(CommonConstant.SC_OK_200));
        r.setData(data);
        return r;
    }

    public static<T> MyResult<T> OK(String msg, T data) {
        MyResult<T> r = new MyResult<T>();
        r.setSuccess(true);
        r.setCode(String.valueOf(CommonConstant.SC_OK_200));
        r.setMessage(msg);
        r.setData(data);
        return r;
    }

    public static<T> MyResult<T> error(String msg, T data) {
        MyResult<T> r = new MyResult<T>();
        r.setSuccess(false);
        r.setCode(String.valueOf(CommonConstant.SC_INTERNAL_SERVER_ERROR_500));
        r.setMessage(msg);
        r.setData(data);
        return r;
    }

    public static MyResult<Object> error(String msg) {
        return error(String.valueOf(CommonConstant.SC_INTERNAL_SERVER_ERROR_500), msg);
    }

    public static MyResult<Object> error(String code, String msg) {
        MyResult<Object> r = new MyResult<Object>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public MyResult<T> error500(String message) {
        this.message = message;
        this.code = String.valueOf(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
        this.success = false;
        return this;
    }
    /**
     * 无权限访问返回结果
     */
    public static MyResult<Object> noauth(String msg) {
        return error(String.valueOf(CommonConstant.SC_JEECG_NO_AUTHZ), msg);
    }

    @JsonIgnore
    private String onlTable;

}
